# Put all necessary libraries here
library(tidyverse)
library(leaflet)
library(tidycensus)
library(tmap)

Due: Friday, March 22nd at 8:30am

Goals of this lab

Problem 1: Mapping Bike Rides in Portland

For this problem we will return to the biketown dataset.

  1. Grab the code from activity 9, Problem 1 to read the data directly from Biketown’s API- make sure to keep the longitude and latitude of the start of each ride (StartLatitude, StartLongitude).
biketown_data <- bind_rows(readr::read_csv("https://s3.amazonaws.com/biketown-tripdata-public/2017_01.csv"),
                           readr::read_csv("https://s3.amazonaws.com/biketown-tripdata-public/2017_07.csv"),
                           readr::read_csv("https://s3.amazonaws.com/biketown-tripdata-public/2017_11.csv")) %>%
  
  select(StartDate, StartTime, StartLatitude, StartLongitude, EndDate, EndTime, Distance_Miles,
         BikeID)
  1. Create an interactive map of the start point of the rides using the leaflet package. Make sure to include a legend and a title. What do you notice about the distribution of rides?
col <- colorBin("PuOr", biketown_data$Distance_Miles, bins = c(0, 1, 2.5, 5, 10, 20))

biketown_data %>% 
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers(lng = ~StartLongitude, lat = ~StartLatitude, color = ~col(biketown_data$Distance_Miles)) %>%
  addLegend(pal = col, values = ~biketown_data$Distance_Miles, group = "circles", title = "Travelled Distance (mi)")

I notice that a lot of the rides are centered around the middle of Portland and are typically short rides (implying to me that it might not be for commuting?). This also tells me that distances are typically pretty short.

  1. Using the lubridate package, create a variable, month, indicating the month of each variable.
biketown_data <- biketown_data %>% mutate(month = month(mdy(StartDate), label = TRUE))

Add this variable to your interactive map using color. Make sure to include a legend and be mindful of your color palette choice. Do ride locations vary by months of the year?

col2 <- colorFactor("Blues", biketown_data$month)

biketown_data %>% 
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers(lng = ~StartLongitude, lat = ~StartLatitude, color = ~col2(biketown_data$month)) %>%
  addLegend(pal = col2, values = ~biketown_data$month, group = "circles", title = "Month of Ride")

It seems like in the summer there are more rides, which makes more sense becuase the weather wouldn’t be a hurdle when riding unlike in Jan/November.

Problem 2: Choropleth Maps

For this problem, I want you to practice creating choropleth maps. Let’s grab some data using tidycensus. Remember that you will have to set up an API key.

  1. Let’s grab data on the median gross rent (B25064_001) from the American Community Survey for Multnomah county, Oregon. I want you to do data pulls at three geography resolutions: county subdivision, tract, and block group.
or_tracts <- get_acs(geography = "tract",
                     year = 2022,
                     variables = c(medrent = "B25064_001"),
                     state = "OR",
                     county = "Multnomah",
                     survey = "acs5",
                     output = "wide",
                     geometry = TRUE)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |======================================================================| 100%
or_county <- get_acs(geography = "county subdivision",
                     year = 2022,
                     variables = c(medrent = "B25064_001"),
                     state = "OR",
                     county = "Multnomah",
                     survey = "acs5",
                     output = "wide",
                     geometry = TRUE)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |======================================================================| 100%
or_block <- get_acs(geography = "block group",
                     year = 2022,
                     variables = c(medrent = "B25064_001"),
                     state = "OR",
                     county = "Multnomah",
                     survey = "acs5",
                     output = "wide",
                     geometry = TRUE)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |======================================================================| 100%
  1. Create three choropleth maps of gross rent, one for each geography resolution. What information can we glean from these maps? Also, which resolution seems most useful for this variable? Justify your answer
tm_shape(or_tracts) +
  tm_polygons(col = "medrentE", style = "quantile", palette = "BuGn", title = "Median Gross Rent") +
  tm_layout(main.title = "Median Gross Rent in Multnomah County by Tracts, 2018-2022",
            main.title.position = "center",
            legend.outside = TRUE)

tm_shape(or_county) +
  tm_polygons(col = "medrentE", style = "quantile", palette = "BuGn", title = "Median Gross Rent") +
  tm_layout(main.title = "Median Gross Rent in Multnomah County by County Subdivisons, 2018-2022",
            main.title.position = "center",
            legend.outside = TRUE)

tm_shape(or_block) +
  tm_polygons(col = "medrentE", style = "quantile", palette = "BuGn", title = "Median Gross Rent") +
  tm_layout(main.title = "Median Gross Rent in Multnomah County by Block Groups, 2018-2022",
            main.title.position = "center",
            legend.outside = TRUE)

It seems that tracts are the most useful as they provide the best compromise with cleanliness and also giving us useful information. We can glean that West Portland tends to be. alittle pricier, and generally rent varies a lot and you can observe some geographical trends. It’s useful to compare by block groups and tracts to identify trends, but if I were to want a singular one, I’d choose tracts still. East Portland (not very east) seems to have the lowest rent.